package jk.fox.node.node;

import jk.fox.common.RpcDict;
import jk.fox.common.RpcURIUtils;

/**
 * @author cuichao
 * @Description: NodeServer
 * @Date: create in 2020/4/23 10:22
 */
public class NodeServer {

    private String schema = RpcDict.DEFAULT_SCHEMA;
    private String hostName;
    private String address;
    private int port = 80;
    private volatile RpcDict.NodeStatus status = RpcDict.NodeStatus.INIT;


    public NodeServer(String address) {
        this.address =address;
        String[] split = address.split(":");
        this.hostName = split[0];
        int port = Integer.parseInt(split[1]);
        this.port = port > 0 ? port : 80;
    }

    public NodeServer(String hostName, int port) {
        this.port = port;
        this.hostName = hostName;
        this.address = RpcURIUtils.getAddress(schema,hostName,port);
    }

    public NodeServer(String schema, String hostName, int port) {
        this.schema = schema;
        this.hostName = hostName;
        this.port = port;
        this.address = RpcURIUtils.getAddress(schema,hostName,port);
    }


    public boolean isAlive() {
        return status == RpcDict.NodeStatus.INIT || status == RpcDict.NodeStatus.SUCCESS;
    }



    public String getSchema() {
        return schema;
    }
    public void setSchema(String schema) {
        this.schema = schema;
    }
    public String getHostName() {
        return hostName;
    }
    public void setHostName(String hostName) {
        this.hostName = hostName;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public int getPort() {
        return port;
    }
    public void setPort(int port) {
        this.port = port;
    }

    public RpcDict.NodeStatus getStatus() {
        return status;
    }
    public void setStatus(RpcDict.NodeStatus status) {
        this.status = status;
    }
}
